<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/5
 * Time: 上午10:19
 */
class NoticeService extends Service{

    public static function indexHeader(){
        return array(
                'id'                => array('text' => 'ID', 'rule' => 'int'),
    			'server_name'       => array('text' => '服务器'),
    			'send_type'      => array('text' => '发送类型'),
    			'title'   => array('text' => '邮件标题'),
    			'content'   => array('text' => '邮件内容','width' => '350px'),
        		'create_user'       => array('text' => '添加人'),
    			'create_time'       => array('text' => '创建时间'),
        		'synchro_time'       => array('text' => '同步时间'),
    			//'action'            => array('text' => '操作')
        );
    }
    
    public static function indexData($params){
    	$user = AccAuth::me();
    	if($user['IsAdmin'] != 1 && $user['AddBy'] != 1){
    		$params['create_user'] = $user['UserID'];
    	}
    	
    	$modNotice = new ModNotice();
    	$result = $modNotice->select($params);
    
    	$sendType = self::sendType();
    	$userList = self::userList();
    	foreach($result as &$v){
    		$v['content'] = '<div style="white-space:normal;">'.$v['content'].'</div>';
    		$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
    		$v['send_type'] = $sendType[$v['send_type']];
    		$v['create_user'] = $userList[$v['create_user']];
    		$v['action']         = ' <a href="'.PubFunc::buildUrl('/oss/?r=notice/delete').'&id='.$v['id'].'" confirm="是否要删除该条服信息?">删除</a>';
    		
    		$id = $v['id'];
    		if($v['synchro_time'] == ''){
    			$v['server_name'] = '<a href="JavaScript:void(0);" name="synchro_one" data-id="'.$v['id'].'" >同步</a>';
    			$v['synchro_time'] = '未同步';
    			$checkbox = '<input value="'.$v['id'].'" name="id" type="checkbox">&nbsp;&nbsp;';
    		
    		}else{
    			$v['synchro_time'] = date('Y-m-d H:i:s', $v['synchro_time']);
    			$checkbox = '<input value="'.$v['id'].'" name="id" disabled="disabled" type="checkbox">&nbsp;&nbsp;';
    		}
    		$v['id'] = $checkbox.$v['id'];
    	}
    	unset($v);
    
    	return $result;
    }
    
    public static function doAdd($params){
    	if(PubFunc::checkEmpty($params, array('send_type','title','content'))){
    		return array('state' => false, 'msg' => '缺少必要参数.');
    	}
    	//$server_array = explode('-', $params['server']);
    	//$params['server'] = $server_array[0];
    	//请求接口，接口成功再入库
    	//$result = self::send($params);
    	$result['state'] = true;
    	if($result['state']){
    		$user = AccAuth::me();
    		$modNotice = new ModNotice();
    		
    		$modNotice->platform      = 'test';
    		$modNotice->game          = $params['g'];
    		//$modNotice->server_id  = $server_array[0];
    		//$modNotice->server_name   = $server_array[1];
    		$modNotice->send_type    = $params['send_type'];
    		$modNotice->title       = $params['title'];
    		$modNotice->content       = $params['content'];
    		$modNotice->create_user   = $user['UserID'];
    		$modNotice->create_time   = time();
    		$modNotice->update_user   = $user['UserID'];
    		$modNotice->update_time   = time();
    		$result = $modNotice->save();
    		return array('state' => true, 'msg' =>'添加成功');
    	}else{
    		return array('state' => false, 'msg' => $result['msg']);
    	}
    
    }
    
    public static function delete($id){
    	if(empty($id)) return false;
    
    	$modNotice = new ModNotice();
    	$modNotice->deleteByPk($id);
    	return true;
    }
    


    public static function send($params){
        $api = new ApiService($params['server']);

        $args = array(
            'sendtype'     => $params['send_type'],           
            'title'         => $params['title'],
            'content'       => $params['content'],
        );
        $result = $api->sendNoticeMail($args);
        return $result;
    }

    public static function sendType(){
        return array(
            '1' => '全服发送',
            '2' => '在线玩家发送'
        );
    }
    
    public static function synchro($ids,$servers){
    	$modNotice = new ModNotice();
    	$serverList = PubServer::getOptions($platform, $game, 'id');
    	 
    	$ids_array = explode(',', $ids);
    	 
    	$server_array = explode(',', $servers);
    	$server_array = array_unique($server_array);
    	 
    	$info_res = '';
    	foreach ($ids_array as $key=>$val){
    		$obj = $modNotice->findByPk($val);
    		$info = $obj->getAttributes();
    		$params = array();
    		$params['send_type'] = $info['send_type'];
    		$params['title'] = $info['title'];
    		$params['content'] = $info['content'];
    		$info_res .= '全服邮件ID为 <font color="red">'.$info['id'].'</font>';
    		$server_id = '';
    		$server_name = '';
    		foreach ($server_array as $key1=>$val1){
    			$params['server'] = $val1;
    			$result = self::send($params);
    			//$result['state'] = true;
    			if($result['state']){
    				$server_id .= ','.$val1;
    				$server_name .= ','.$serverList[$val1];
    				$info_res .= ' 发送服务器 <font color="red">'.$serverList[$val1].' </font>成功<br/>';
    			}else{
    				$info_res .= ' 发送服务器 <font color="red">'.$serverList[$val1].' </font>失败，失败原因'.$result['msg'].'<br/>';
    			}
    		}
    		
    		if($server_id != '' && $server_name != ''){
    			$server_id = substr($server_id, 1);
    			$server_name = substr($server_name, 1);
    			$update = array();
    			$update['server_id']     = $server_id;
    			$update['server_name']     = $server_name;
    			$update['synchro_time']  = time();
    			$modNotice->updateByPk($info['id'], $update);
    		}
    		
    	}
    	return $info_res;
    	 
    }


}